﻿//************************************************************************************************
// Copyright © 2012 Steven M. Cohn. All Rights Reserved.
//
//************************************************************************************************ 

namespace CookieChipper
{
	using System;
	using System.Windows.Forms;


	internal partial class BackupPage : WizardPage
	{
		public BackupPage ()
		{
			InitializeComponent();

			base.Description = "Name your backup file and location";

			locationBox.Text = folderBrowserDialog.SelectedPath =
				Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
		}


		private void DoBrowse (object sender, EventArgs e)
		{
			DialogResult result = folderBrowserDialog.ShowDialog(this);
			if (result == DialogResult.OK)
			{
				locationBox.Text = folderBrowserDialog.SelectedPath;
			}
		}

	
		private void DoTextChanged (object sender, EventArgs e)
		{
			string path = locationBox.Text.Trim();
			PageState = (path.Length == 0 ? PageStates.Empty : PageStates.Ready);
		}


		private void DoVisibleChanged (object sender, EventArgs e)
		{
			DoTextChanged(sender, e);
			locationBox.Focus();
		}


		public override bool Execute ()
		{
			try
			{
				using (var backup = new BackupProvider(locationBox.Text))
				{
					backup.OpenWriter();
					backup.Backup();

					base.Information = backup.FullPath;
				}
			}
			catch (Exception exc)
			{
				base.Information = exc.Message;
				return false;
			}
			return true;
		}
	}
}
